Skip to content

Conversation

@ayush-singh-26
Copy link

@ayush-singh-26 ayush-singh-26 commented Oct 10, 2025

🧩 LeetCode 207 — Course Schedule

🧠 Intuition

The problem is about determining if all courses can be completed given their prerequisites.
Think of each course as a node in a directed graph, and each prerequisite pair [a, b] as a directed edge from b → a — meaning b must be completed before a.
If there’s a cycle, it means a course depends on itself (directly or indirectly), so it’s impossible to complete all courses.
Hence, the task reduces to detecting if the directed graph has a cycle.


🚀 Approach

We use Kahn’s Algorithm (Topological Sorting using BFS) to determine if all nodes can be visited without a cycle.

  1. Build the Graph

    • Create an adjacency list adj for the courses.
    • Maintain an inDegree array to store how many prerequisites each course has.
  2. Initialize Queue

    • Add all courses with inDegree == 0 (no prerequisites) into a queue.
  3. Process Courses

    • While the queue isn’t empty:
      • Pop a course x from the queue and increment a count of completed courses.
      • For each dependent course in adj[x], decrement its inDegree.
      • If any course’s inDegree becomes 0, push it into the queue.
  4. Check for Cycles

    • If count == V (all courses processed), return true.
    • Otherwise, a cycle exists — return false.

💻 Code Solution (C++)

class Solution {
public:
    bool canFinish(int V, vector<vector<int>>& prerequisites) {
        vector<vector<int>> adj(V);
        vector<int> inDeg(V);
        
        for (int i = 0; i < prerequisites.size(); i++) {
            int a = prerequisites[i][0];
            int b = prerequisites[i][1];
            adj[b].push_back(a);
            inDeg[a]++;
        }

        queue<int> q;
        for (int i = 0; i < V; i++) {
            if (inDeg[i] == 0)
                q.push(i);
        }

        int count = 0;
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            count++;

            for (int i = 0; i < adj[x].size(); i++) {
                inDeg[adj[x][i]]--;
                if (inDeg[adj[x][i]] == 0)
                    q.push(adj[x][i]);
            }
        }

        return count == V;
    }
};

✅ By submitting this PR, I confirm that:

  • This is my original work not totally AI generated
  • I have tested the solution thoroughly on LeetCode
  • I have maintained proper PR description format
  • This is a meaningful contribution, not spam

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising the PR, the owner will be review it soon' keep patience, keep contributing>>>!!! make sure you have star ⭐ the repo

@SjxSubham SjxSubham linked an issue Oct 10, 2025 that may be closed by this pull request
4 tasks
@SjxSubham SjxSubham added the hacktoberest-accepted hacktoberfest-accepted label Oct 10, 2025
@SjxSubham SjxSubham changed the title Added C++ solution for course schedule leetcode problem 206.Course Schedule.cpp Oct 10, 2025
@SjxSubham
Copy link
Owner

Hi @ayush-singh-26 thanks for the PR...

Happy HAcktober... keep contributing ....

@SjxSubham SjxSubham merged commit 97e83c8 into SjxSubham:main Oct 10, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberest-accepted hacktoberfest-accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

207. Course Schedule

2 participants